import {
  Inject,
  Controller,
  Get,
  Query,
  Post,
  Body,
  Config
} from "@midwayjs/core";
import { Context } from "@midwayjs/koa";
import { AuthService } from "../service/auth.service";
import { ApiTags } from "@midwayjs/swagger";
import { Utils } from "../utils/index";

@ApiTags("登录鉴权")
@Controller()
export class AuthController {
  @Inject()
  ctx: Context;

  @Inject()
  authService: AuthService;

  @Inject()
  utils: Utils;

  @Config("jwtCookieKey")
  jwtCookieKey: string;

  @Get("/auth-api/current", { summary: "获取当前用户信息" })
  async get_current_user() {
    const token = this.ctx.cookies.get(this.jwtCookieKey);
    const user = await this.authService.get_current_user(token);
    if (user.success) {
      return this.utils.response.success({
        data: user.data
      });
    } else {
      return this.utils.response.fail();
    }
  }

  /**
   * 登录 GET方式，测试用
   * @param account
   * @param password
   * @param captchaId
   * @param captchaCode
   * @returns
   */
  @Get("/auth/login", { summary: "系统登录， 用于测试" })
  async get_login(
    @Query("account") account: string,
    @Query("password") password: string,
    @Query("captchaId") captchaId: string,
    @Query("captchaCode") captchaCode: string
  ) {
    const res = await this.authService.login(
      account,
      password,
      captchaId,
      captchaCode,
      true
    );

    if (res.success) {
      return {
        ...this.utils.response.success(res),
        ...Utils.RESP_CODE_MAP.LOGIN_SUCCESS
      };
    } else {
      return {
        ...res,
        ...Utils.RESP_CODE_MAP.LOGIN_FAILT_USERNAME_PASSWORD_ERROR
      };
    }
  }

  /**
   * 系统登录
   * @param account
   * @param password
   * @param captchaId
   * @param captchaCode
   * @returns
   */
  @Post("/auth/login", { summary: "系统登录" })
  async post_login(
    @Body("account") account: string,
    @Body("password") password: string,
    @Body("captchaId") captchaId: string,
    @Body("captchaCode") captchaCode: string
  ) {
    if (!account || !password || !captchaId || !captchaCode) {
      return this.utils.response.fail({ message: "参数错误" });
    }

    const res = await this.authService.login(
      account,
      password,
      captchaId,
      captchaCode
    );

    if (res.success) {
      return {
        ...this.utils.response.success(res),
        ...Utils.RESP_CODE_MAP.LOGIN_SUCCESS
      };
    } else {
      return {
        ...res,
        ...Utils.RESP_CODE_MAP.LOGIN_FAILT_USERNAME_PASSWORD_ERROR
      };
    }
  }

  /**
   * 校验验证码 测试用
   * @param id
   * @param code
   * @returns
   */
  @Get("/auth/check", { summary: "验证码校验，用于测试" })
  async checkCaptcha(
    @Query("captchaId") captchaId: string,
    @Query("captchaCode") captchaCode: string
  ) {
    if (!captchaId || !captchaCode)
      return this.utils.response.fail({ message: "参数错误" });
    const isok = await this.authService.checkCaptcha(captchaId, captchaCode);
    return this.utils.response.success({ data: isok });
  }

  /**
   * 获取验证码
   * @returns
   */
  @Get("/auth/captcha", { summary: "获取验证码" })
  async getCaptcha() {
    const data = await this.authService.createCaptcha();
    return this.utils.response.success({ data });
  }
}
